package com.zhkj.txg.module.product.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.zhkj.lib.base.BaseViewModel
import com.zhkj.lib.http.BaseResponse
import com.zhkj.lib.http.HttpResponse
import com.zhkj.lib.http.ViewModelSubscribeListener
import com.zhkj.txg.module.product.entity.ProductResponse
import com.zhkj.txg.module.product.model.ProductManager
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.module.product.entity.ProductResponse2

class ProductViewModel(application: Application) : BaseViewModel(application) {

    private val productManager = ProductManager()
    val addCartLiveData = MutableLiveData<HttpResponse<BaseResponse>>()
    val productLiveData = MutableLiveData<HttpResponse<ProductResponse>>()
    val productLiveData2 = MutableLiveData<HttpResponse<ProductResponse2>>()
    val collectProductLiveData = MutableLiveData<HttpResponse<BaseResponse>>()

    /**
     * 获取产品数据
     */
    fun getProduct(id: Int, shopId: Int) {
        val token = AccountManager.getToken()
        val listener = ViewModelSubscribeListener(productLiveData)
        val product = productManager.product(token, id, shopId, listener)
        dispose.addAll(product)
    }

    /**
     * 获取产品数据
     */
    fun getProduct2(id: Int, shopId: Int) {
        val token = AccountManager.getToken()
        val listener = ViewModelSubscribeListener(productLiveData2)
        val product = productManager.product2(token, id, shopId, listener)
        dispose.addAll(product)
    }

    /**
     * 添加购物车
     */
    fun addCart(map: Map<String, String>) {
        val listener = ViewModelSubscribeListener(addCartLiveData)
        val addCart = productManager.addCart(map, listener)
        dispose.add(addCart)
    }

    /**
     * 收藏/取消收藏商品
     */
    fun collectGoods(
        token: String, shopId: Long, goodsId: Long, type: String
    ) {
        val listener = ViewModelSubscribeListener(collectProductLiveData)
        val collectProduct = productManager.collectProduct(token, shopId, goodsId, type, listener)
        dispose.add(collectProduct)
    }
}